//
//  NewViewController.m
//  HaoHaiSpell
//
//  Created by haohai on 16/6/17.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "NewViewController.h"
#import "FourthSectionCell.h"
#import "HeaderView.h"
@interface NewViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)UICollectionView *newuserCollectionView;
@property (nonatomic,strong)NSMutableArray *allDataArray;
@property (nonatomic,strong)UIView *headView;

@end

@implementation NewViewController

#pragma mark - 懒加载
-(UICollectionView *)newuserCollectionView{

    if (!_newuserCollectionView) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing=10;
        layout.minimumInteritemSpacing=10;
        layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
        //      layout.itemSize=CGSizeMake(kWidth-20, (kHeight-30)/3);
        _newuserCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
        _newuserCollectionView.backgroundColor=[UIColor whiteColor];
        _newuserCollectionView.delegate=self;
        _newuserCollectionView.dataSource=self;
    }
    return _newuserCollectionView;
}

-(NSMutableArray *)allDataArray{

    if (!_allDataArray) {
        _allDataArray = [NSMutableArray new];
    }
    return _allDataArray;
}


-(UIView *)headView{
    
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 3)];
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 6)];
        imageView.image = [UIImage imageNamed:@"contentimg.jpg"];
        [_headView addSubview:imageView];
<<<<<<< HEAD
        UIImage *image_0 = [UIImage imageNamed:@"qhb.png"];
        image_0 = [image_0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIButton *btn = [self createButtonWithFrame:CGRectMake(60, kHeight / 6 + 15, kWidth / 4, kWidth / 4) title:@"抢红包" slector:@selector(redEnvelope:) image:image_0];
        btn.layer.cornerRadius = kWidth / 8;
        btn.layer.masksToBounds = YES;
        UIImage *image1 = [UIImage imageNamed:@"qyh.png"];
        image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIButton *btn1 = [self createButtonWithFrame:CGRectMake(kWidth / 4 + 150, kHeight / 6 + 15, kWidth / 4, kWidth / 4) title:@"优惠" slector:@selector(preferential:) image:image1];
        btn1.layer.cornerRadius = kWidth / 8;
        btn1.layer.masksToBounds = YES;
=======
        UIButton *btn = [self createButtonWithFrame:CGRectMake(60, kHeight / 6 + 15, kWidth / 4, kWidth / 4) title:@"抢红包" slector:@selector(redEnvelope:) image:[UIImage imageNamed:@"Corner-1.png"]];
        btn.layer.cornerRadius = kWidth / 8;
        btn.layer.masksToBounds = YES;
        btn.backgroundColor = [UIColor redColor];
        UIButton *btn1 = [self createButtonWithFrame:CGRectMake(kWidth / 4 + 150, kHeight / 6 + 15, kWidth / 4, kWidth / 4) title:@"优惠" slector:@selector(preferential:) image:[UIImage imageNamed:@"Corner-1.png"]];
        btn1.layer.cornerRadius = kWidth / 8;
        btn1.layer.masksToBounds = YES;
        btn1.backgroundColor = [UIColor redColor];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        [_headView addSubview:btn];
        [_headView addSubview:btn1];
    }
    return _headView;
}


#pragma mark - 自定义
-(UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title slector:(SEL)selector image:(UIImage *)btImage{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setImage:btImage forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setFrame:frame];
    [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

//抢红包按钮回调
-(void)redEnvelope:(UIButton *)sender{

    NSLog(@"抢红包");
}


//抢优惠按钮回调
-(void)preferential:(UIButton *)sender{
    
    NSLog(@"抢优惠");
}



#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.newuserCollectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    //注册单元格
    [self.newuserCollectionView registerNib:[UINib nibWithNibName:@"FourthSectionCell" bundle:nil] forCellWithReuseIdentifier:@"fourthCell"];
    //注册头部视图
    [self.newuserCollectionView registerNib:[UINib nibWithNibName:@"HeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.newuserCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"firstView"];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }else if (section == 1){
        return 5;
    }else{
    
        return 2;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    FourthSectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fourthCell" forIndexPath:indexPath];
    cell.prePriceLabel.lineType = LineTypeMiddle;
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


    return CGSizeMake((kWidth- 30) / 2, kHeight / 3);
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return CGSizeMake(kWidth, kHeight / 3);
    }else{
    
        return CGSizeMake(kWidth, kHeight / 18);
    }
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        UICollectionReusableView *firstView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"firstView" forIndexPath:indexPath];
        [firstView addSubview:self.headView];
        return firstView;
    }else{
        HeaderView *view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        if (indexPath.section == 1) {
            view.titleLabel.text = @"新人专享";
        }else {
        
        view.titleLabel.text = @"互动游戏";
        }
        return view;
    }
}



@end
